package com.javaenzk.wfx.manager.service.impl;


import com.github.wxpay.sdk.MyWXConfig;
import com.github.wxpay.sdk.WXPay;
import com.javaenzk.wfx.manager.dao.OrderMapper;
import com.javaenzk.wfx.manager.po.OrderInfo;
import com.javaenzk.wfx.manager.service.IOrderService;
import com.javaenzk.wfx.manager.vo.JsonResultVO;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderServiceImpl{

    @Resource
    private OrderMapper orderMapper;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addOrder(@RequestBody OrderInfo orderInfo){

        String code_url="";

        try {
            orderMapper.addOrder(orderInfo);

            //2、微信下单
            //---------微信预付单----------------------
            //微信API
            WXPay wxPay = new WXPay(new MyWXConfig());
            Map<String, String> data = new HashMap<String, String>();

            //商品的名称
            data.put("body", "牛仔裤/面膜");
            //订单号（自定义），不能重复
            data.put("out_trade_no", orderInfo.getOrderId());
            //设备信息
            data.put("device_info", "PC");
            //币种
            data.put("fee_type", "CNY");
            //订单金额（单位是分）
            data.put("total_fee", "1");
            //设备IP
            data.put("spbill_create_ip", "123.12.12.123");
            //回调接口（微信支付完之后的通知接口）
            data.put("notify_url", "http://fbj28w.natappfree.cc/order/notify");
            //支付模式native或者jsapi
            data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
            //产品ID自定义
            data.put("product_id", "12");

            //下预付单（下给微信了）
            //返回值是一个支付短链接
            Map<String, String> resp = wxPay.unifiedOrder(data);
            //支付的短链接
            code_url = resp.get("code_url");
            //---------微信预付单----------------------

        }catch (Exception e){
            e.printStackTrace();

        }

       return code_url;

    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public JsonResultVO updateOrderState(String orderId){

        JsonResultVO jsonResultVO = new JsonResultVO();

        try {
            orderMapper.updateOrderState(orderId);
            jsonResultVO.setCode(1);
        }catch (Exception e){
            e.printStackTrace();
            jsonResultVO.setCode(0);
        }

        return jsonResultVO;
    }
}
